package top.someapp.dxf2dsvg.cad;

import com.aspose.cad.Image;
import com.aspose.cad.fileformats.cad.CadImage;
import top.someapp.dxf2dsvg.base.DxfException;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author zwz
 * Created on 2020-04-22
 */
public class AutoCadFileLoader {
    private static AutoCadFileLoader cadFileLoader = new AutoCadFileLoader();
    private File file;

    public static AutoCadFileLoader getLoader() {
        return cadFileLoader;
    }

    public AutoCadImage load(@Nonnull File cadFile) throws IOException {
        this.file = cadFile;
        return load();
    }

    private AutoCadImage load() throws FileNotFoundException {
        Image cadImage = Image.load(new FileInputStream(file));
        if (!(cadImage instanceof CadImage)) {
            throw new DxfException("Can not load file: " + file.getAbsolutePath() +
                    "\nplease ensure it's a .dwg/.dxf AutoCad file!");
        }
        return new AutoCadImage(file, (CadImage) cadImage);
    }
}
